package com.codefish.framework.spring.aop.framework.adapter;

import com.codefish.framework.spring.aop.IBeforeAdvice;
import com.codefish.framework.spring.aop.IMethodBeforeAdvice;
import com.codefish.framework.spring.core.utils.AssertUtils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 实现方法执行前拦截调用
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/20 下午 09:44
 */
public class MethodBeforeInterceptor implements MethodInterceptor, IBeforeAdvice {
    IMethodBeforeAdvice advice;

    public MethodBeforeInterceptor(IMethodBeforeAdvice methodBeforeAdvice) {
        AssertUtils.notNull(methodBeforeAdvice, "methodBeforeAdvice不能为空");
        this.advice = methodBeforeAdvice;
    }

    public MethodBeforeInterceptor() {

    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        advice.before(invocation.getMethod(), invocation.getArguments(), invocation.getThis());
        return invocation.proceed();
    }

}
